import useMemberStore from '@/stores/member';
import { t } from '@/locale';
import { getToken, getAppChannel, getSiteId, redirect, currRoute, isUrl } from './common';
import { getPlatform } from './formatting';
import qs from 'qs';
import { useLogin } from '@/hooks/useLogin'
// 1. 初始化全局计数器（记录未完成的请求数）
let loadingCount = 0;
interface RequestConfig {
	showErrorMessage?: boolean;
	showSuccessMessage?: boolean;
	isYuDao?: boolean;
	isYuDaoAdmin?: boolean;
	isPutQs?: boolean

}

interface RequestOptions extends UniNamespace.RequestOptions, RequestOptions { }

class Request {
	private baseUrl: string;
	private yudaoBaseUrl: string;
	private yudaoAdminBaseUrl: string;



	private config: RequestOptions = {
		url: '',
		header: {}
	};

	constructor() {
		// #ifdef H5
		this.baseUrl = import.meta.env.VITE_APP_BASE_URL || `${location.origin}/api/`;
		this.yudaoBaseUrl = import.meta.env.VITE_YUDAO_APP_BASE_URL;
		this.yudaoAdminBaseUrl = import.meta.env.VITE_YUDAO_APP_ADMIN_BASE_URL
		// #endif
		// #ifndef H5
		this.baseUrl = import.meta.env.VITE_APP_BASE_URL;
		this.yudaoBaseUrl = import.meta.env.VITE_YUDAO_APP_BASE_URL;
		this.yudaoAdminBaseUrl = import.meta.env.VITE_YUDAO_APP_ADMIN_BASE_URL

		// #endif
		// #ifndef APP-PLUS
		this.baseUrl = "https://metonapi.meton.vip/api/";
		this.yudaoBaseUrl = "https://metonapi.meton.vip/app-api/";
		this.yudaoAdminBaseUrl = "https://metonapi.meton.vip/admin-api/"

		// #endif
		this.baseUrl.substr(-1) != '/' && (this.baseUrl += '/');
		// this.config.header['platform'] = getPlatform().name
		this.config.header['platform'] = getPlatform().name
		this.config.header['terminal'] = getPlatform().terminal


		this.config.header['tenant-type'] = import.meta.env.VITE_TENANT_TYPE
		try {
			if (process.env.NODE_ENV == 'development') {
				this.config.header[import.meta.env.VITE_REQUEST_HEADER_SITEID_KEY] = getSiteId(import.meta.env.VITE_SITE_ID || uni.getStorageSync('wap_site_id'));
			} else {
				this.config.header[import.meta.env.VITE_REQUEST_HEADER_SITEID_KEY] = getSiteId(import.meta.env.VITE_SITE_ID || uni.getStorageSync('wap_site_id'));
			}
			this.config.header[import.meta.env.VITE_REQUEST_HEADER_CHANNEL_KEY] = getAppChannel();
		} catch (e) { }
	}

	/**
	 * 请求拦截器
	 */
	private requestInterceptors() {
		// 携带token site-id
		try {
			getToken() && (this.config.header[import.meta.env.VITE_REQUEST_HEADER_TOKEN_KEY] = getToken());
			this.config.header[import.meta.env.VITE_REQUEST_HEADER_CHANNEL_KEY] = getAppChannel();
			if (process.env.NODE_ENV == 'development') {
				this.config.header[import.meta.env.VITE_REQUEST_HEADER_SITEID_KEY] = getSiteId(import.meta.env.VITE_SITE_ID || uni.getStorageSync('wap_site_id'));
			} else {
				this.config.header[import.meta.env.VITE_REQUEST_HEADER_SITEID_KEY] = getSiteId(import.meta.env.VITE_SITE_ID || uni.getStorageSync('wap_site_id'));
			}
		} catch (e) { }
	}

	public get(url: string, data: AnyObject = {}, config: RequestConfig = {}) {
		return this.request('GET', url, data, config);
	}

	public post(url: string, data: AnyObject = {}, config: RequestConfig = {}) {
		return this.request('POST', url, data, config);
	}

	public put(url: string, data: AnyObject = {}, config: RequestConfig = {}) {
		return this.request('PUT', url, data, config);
	}

	public delete(url: string, data: AnyObject = {}, config: RequestConfig = {}) {
		return this.request('DELETE', url, data, config);
	}
	/**
	 * 发送上传请求
	 */
	public upload(url: string, data: AnyObject = {}, config: RequestConfig = {}) {
		this.requestInterceptors();

		const params = Object.assign(uni.$u.deepClone(this.config), config, {
			url: this.baseUrl + url,
			...data
		});
		if (config.isYuDao) {
			params.url = this.yudaoBaseUrl + url;
			params.header['tenant-id'] = getSiteId(import.meta.env.VITE_SITE_ID || uni.getStorageSync('wap_site_id'))
		}
		if (config.isYuDaoAdmin) {
			params.url = this.yudaoAdminBaseUrl + url;
			params.header['tenant-id'] = getSiteId(import.meta.env.VITE_SITE_ID || uni.getStorageSync('wap_site_id'))
		}
		uni.showLoading({
			mask: true
		})

		return new Promise((resolve, reject) => {
			uni.uploadFile({
				...params,
				success: (res) => {
					uni.hideLoading()

					const data = JSON.parse(res.data);
					if (config && (config.isYuDao || config.isYuDaoAdmin)) {
						if (data.code == 0) {
							config.showSuccessMessage && uni.showToast({ title: data.msg, icon: 'none' });
							resolve(data);
						} else {
							if (data.code != 0) {
								if (config.showErrorMessage !== false) uni.showToast({ title: data.msg, icon: 'none' });
							} else {
								this.handleAuthError(data.code);
							}
							reject(data);
						}

					} else {
						if (data.code == 1) {
							config.showSuccessMessage && uni.showToast({ title: data.msg, icon: 'none' });
							resolve(data);
						} else {
							if (data.code == 400) {
								if (config.showErrorMessage !== false) uni.showToast({ title: data.msg, icon: 'none' });
							} else {
								this.handleAuthError(data.code);
							}
							reject(data);
						}

					}


				},
				fail: (res) => {
					uni.hideLoading()

					reject(res);
				}
			});
		});
	}

	/**
	 * 发送请求
	 */
	private request(method: string, url: string, data?: AnyObject, config: RequestConfig = {}, needLoading = true) {
		this.requestInterceptors();

		const params = Object.assign(uni.$u.deepClone(this.config), config, {
			url: this.baseUrl + url,
			method
		});

		if (config.isYuDao) {
			params.url = this.yudaoBaseUrl + url;
			params.header['tenant-id'] = getSiteId(uni.getStorageSync('wap_tentant_id'))
		}
		if (config.isYuDaoAdmin) {
			params.url = this.yudaoAdminBaseUrl + url;
			params.header['tenant-id'] = getSiteId(uni.getStorageSync('wap_tentant_id'))
		}

		if (params.method.toUpperCase() == 'GET' && !uni.$u.test.empty(data)) {
			params.url += '?' + qs.stringify(data);
		} else if (params.method.toUpperCase() == 'PUT' && config.isPutQs && !uni.$u.test.empty(data)) {
			params.url += '?' + qs.stringify(data);
		} else {
			params.data = data;
		}

		// 2. 发起请求前：计数+1，若首次请求则显示 Loading
		if (needLoading) {
			loadingCount += 1;
			// 仅当计数器从 0→1 时，调用 showLoading（避免重复显示）
			if (loadingCount === 1) {
				uni.showLoading({
					mask: true, // 遮罩层，防止用户点击穿透（可选）
					fail: (err) => {
						console.error('Loading 显示失败：', err);
					}
				});
			}
		}
		return new Promise((resolve, reject) => {
			uni.request({
				...params,
				success: (res) => {
					const data = res.data;

					if (config && (config.isYuDao || config.isYuDaoAdmin)) {
						// 如果是芋道的接口

						if (data.code == 0) {
							config.showSuccessMessage && uni.showToast({ title: data.msg, icon: 'none' });

							resolve(data);
						} else {

							if (data.code !== 0) {
								if (config.showErrorMessage !== false) uni.showToast({ title: data.msg, icon: 'none' });
							}
							if (data.code == 401) {
								useMemberStore().logout();
								useLogin().setLoginBack({ url: '/app/pages/index/index' }) // 设置登录后的返回页


							} else {
								uni.showToast({ title: data.msg, icon: 'none' })
								this.handleAuthError(data.code);
							}
							reject(data);
						}

					} else {
						if (data.code == 1) {
							config.showSuccessMessage && uni.showToast({ title: data.msg, icon: 'none' });

							resolve(data);
						} else {
							if (data.code == 0 || data.code == 400) {
								if (config.showErrorMessage !== false) uni.showToast({ title: data.msg, icon: 'none' })
							} else {
								this.handleAuthError(data.code)
							}
							reject(data);
						}
					}


				},
				fail: (res) => {

					reject(res);
				},
				complete: (res) => {
					if (needLoading) {
						loadingCount -= 1;
						// 仅当计数器从 1→0 时，调用 hideLoading（避免重复隐藏报错）
						if (loadingCount === 0) {
							uni.hideLoading();
						}
					}
					this.handleRequestFail(res);
				}
			});
		});
	}

	private handleAuthError(code: number) {
		switch (code) {
			case 401:
				useMemberStore().logout();
				useLogin().setLoginBack({ url: '/app/pages/index/index' }) // 设置登录后的返回页

				break;
			case 402:
				if (currRoute().indexOf('app/pages/index/close') != -1) return;
				redirect({ url: '/app/pages/index/close', mode: 'reLaunch' });
				break;
			case 403:
				if (currRoute().indexOf('app/pages/index/nosite') != -1) return;
				redirect({ url: '/app/pages/index/nosite', mode: 'switchTab' });
				break;
		}
	}

	private handleRequestFail(res) {
		if (res.errMsg && res.errMsg == 'request:ok') {
			if (typeof res.data == 'string') {
				// #ifdef H5
				const err = (isUrl(this.baseUrl) ? this.baseUrl : location.origin + this.baseUrl) + t('baseUrlError');
				// #endif
				// #ifndef H5
				const err = this.baseUrl + t('baseUrlError');
				// #endif
				uni.showToast({ icon: 'none', title: err });
				return;
			}
		}
		if (res.errMsg == 'request:fail') {
			uni.showToast({ icon: 'none', title: this.baseUrl + t('requestFail') });
			return;
		}
		if (res.errMsg && res.errMsg == 'request:fail url not in domain list') {
			uni.showToast({ icon: 'none', title: this.baseUrl + t('notInDomainList') });
			return;
		}
	}
}

export default new Request();
